package com.yanqu.road.server.cmd.newmonopolymkt.office;

import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_OFFICE_ACTION, desc = "进入大宋榷署 抢夺/撤回位置")
public class MonopolyMarketOfficeActionCmd extends AbstractCommand {

    int type;
    int posIndex;
    int seatTime;
    long seatUserId;
    NewMonopolyMarketProto.NewMonopolyMarketOfficeActionReqMsg req;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        req = NewMonopolyMarketProto.NewMonopolyMarketOfficeActionReqMsg.parseFrom(packet.getBytes());
        type = req.getType();
        posIndex = req.getPosIndex();
        if(req.hasSeatTime()){
            seatTime = req.getSeatTime();
        }
        if(req.hasSeatUserId()){
            seatUserId = req.getSeatUserId();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(type != 0 && type != 1){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        if(!NewMonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(!NewMonopolyMarketMgr.isSystemOpenTime(eMonopolyMarketResType.OFFICE_NEW.getValue())){
            ret = GameErrorCode.E_MONOPOLY_MARKET_SYSTEM_NO_OPEN;
            return;
        }
        ret = player.getModule(NewMonopolyMarketModule.class).officeAction(type, posIndex, seatTime, seatUserId, req);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            NewMonopolyMarketProto.NewMonopolyMarketOfficeActionRespMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketOfficeActionRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(Protocol.U_NEW_MONOPOLY_MARKET_OFFICE_ACTION, builder);
        }
    }
}
